import java.util.Scanner;

public class tic2 {

    static char[] chess = new char[9];
    static Scanner input = new Scanner(System.in);


    public static void main(String[] args) {
        //初始化
        StartGame();


        //这里默认玩家先下棋
        char who = 'x';
        //当双方都没赢 并棋盘有空 游戏继续
        while (!Win('x') && !Win('o') && !isFull()) {
            switch (who) {
                case 'x':
                    playerGo();
                    who = 'o';
                    break;
                case 'o':
                    computerGo();
                    who = 'x';
                    break;
            }
        }
        //最终的结果界面 显示结果
        if (Win('o')) {
            System.out.println("Computer win！");
        } else if (Win('x')) {
            System.out.println("player win!");
        } else {
            System.out.println("it ends in a draw！");
        }

    }

    private static void computerGo() {
        int best = 0; //记录最佳位置
        int bestScore = -1000;
        int score;

        for (int i = 0; i < chess.length; i++) {
            if (chess[i] == '-') {
                chess[i] = 'o';//电脑尝试下这里
                score = bestInput('x', -10000, 10000);//core
                System.out.println("电脑在"+i+"的分数："+score);
                if (score >= bestScore) {
                    bestScore = score;
                    best = i;
                    if (score == 1) {
                        //表示一条路可走到底赢 再次剪枝
                        break;
                    }
                }
                chess[i] = '-'; //只是试探并没下 重新置为空
            }
        }

        //真正地下棋
        chess[best] = 'o';
        Print();

    }

    /**
     * 核心
     *
     * @param who   用于区分max 和 min
     * @param alpha
     * @param beta
     * @return
     */
    private static int bestInput(char who, int alpha, int beta) {
        //递归先写结束
        if (Win('o')) {
            //如果电脑赢了 (电脑取max)
            return 1;
        } else if (Win('x')) {
            return -1;
        } else if (isFull()) {
            //棋盘满了 平局
            return 0;
        } else {
            int score;
            for (int i = 0; i < chess.length; i++) {
                if (chess[i] == '-') {
                    chess[i] = who; //尝试计算下这一步
                    //并切换选手 继续计算
                    score = (who == 'x') ? bestInput('o', alpha, beta) : bestInput('x', alpha, beta);
                    //因为是试下 所以要还原
                    chess[i] = '-';

                    if (who == 'o') {
                        //max层 电脑
                        if (score >= alpha) {
                            alpha = score;
                        }
                        if (alpha >= beta) {
                            //直接剪枝 返回给的是min ，min会选小也就是beta
                            return beta;
                        }
                    } else {
                        //min层
                        if (score < beta) {
                            beta = score;
                        }
                        if (alpha >= beta) {
                            //直接剪枝 返回
                            return alpha;
                        }
                    }
                }
            }

            //最终返回
            if (who == 'o')
                return alpha;
            else
                return beta;
        }


    }

    private static void playerGo() {

        System.out.println("棋盘位置为0-8,请输入：");
        int a = input.nextInt();
        if (a >= 0 && a <= 8 && chess[a] == '-') {


            chess[a] = 'x';
            Print();
        } else {
            //输入错误给予提示
            System.out.println("Your character input is wrong, please re-enter!");
            playerGo();

        }
    }

    public static void StartGame() {
        //初始化棋盘
        for (int i = 0; i < chess.length; i++) {
            chess[i] = '-';
        }

        System.out.println("开始游戏");
        Print();//打印期盼

    }

    /**
     * 打印棋盘
     */
    private static void Print() {
        System.out.println("**********");
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.print("| ");
                System.out.print(chess[i * 3 + j] + " ");
                if (j == 2)
                    System.out.println("|");
            }
            if (i == 2) System.out.println("**********");
        }

    }


    //判断输赢
    public static Boolean Win(char player) {
        if (chess[0] == player && chess[1] == player && chess[2] == player)
            return true;
        if (chess[3] == player && chess[4] == player && chess[5] == player)
            return true;
        if (chess[6] == player && chess[7] == player && chess[8] == player)
            return true;
        if (chess[0] == player && chess[3] == player && chess[6] == player)
            return true;
        if (chess[1] == player && chess[4] == player && chess[7] == player)
            return true;
        if (chess[2] == player && chess[5] == player && chess[8] == player)
            return true;
        if (chess[0] == player && chess[4] == player && chess[8] == player)
            return true;
        if (chess[2] == player && chess[4] == player && chess[6] == player)
            return true;

        return false;
    }

    //判断棋盘是否还有地方能下棋子
    public static boolean isFull() {
        //判断棋盘是否为空
        for (int i = 0; i < chess.length; i++) {
            if (chess[i] == '-')
                return false; //棋盘还有位置可以下 不为空
        }
        return true;
    }
}
